﻿using BCCommon;
using BCCommon.OAEnums.Workflow;
using BCData.OA.Workflow.WorkflowActivityInstanceBusiness;
using BCDto.OA.Workflow.WorkflowActivityInstance;
using BCDto.OA.Workflow.WorkflowActivityInstanceReference;
using BCEntity;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System.Linq;

namespace BCService.OA.Workflow.WorkflowActivityInstanceBusiness
{
    public class WorkflowActivityInstanceBusinessService : IWorkflowActivityInstanceBusinessService
    {
        private readonly IWorkflowActivityInstanceBusinessData workflowActivityInstanceBusinessData;
        private readonly IRedisService redisService;
      
        public WorkflowActivityInstanceBusinessService(IWorkflowActivityInstanceBusinessData workflowActivityInstanceBusinessData,
            IRedisService redisService)
        {
            this.workflowActivityInstanceBusinessData = workflowActivityInstanceBusinessData;
            this.redisService = redisService;
        }

        /// <summary>
        /// 根据活动实例Id获取信息
        /// </summary>
        /// <param name="workflowActivityInstanceId">活动实例Id</param>
        /// <returns></returns>
        public WorkflowActivityInstanceDto GetInstanceInfo(long workflowActivityInstanceId)
        {
            var instanceResult = this.workflowActivityInstanceBusinessData.GetInstanceInfo(workflowActivityInstanceId).GetAwaiter().GetResult();
            var instanceEntity = instanceResult.Item2;
            instanceEntity.Steps = instanceResult.Item1.ToList();
            instanceEntity.ExtendMethods = this.workflowActivityInstanceBusinessData.GetExtendMethods(workflowActivityInstanceId).GetAwaiter().GetResult().ToList();
            return instanceEntity.As<WorkflowActivityInstanceDto>();
        }

        /// <summary>
        /// 根据工作流Key获取活动实例信息
        /// </summary>
        /// <param name="workflowActivityId">活动Id</param>
        /// <returns></returns>
        public WorkflowActivityInstanceDto GetByWorkflowActivityId(long workflowActivityId)
        {
            return workflowActivityInstanceBusinessData.GetByWorkflowActivityId(workflowActivityId).GetAwaiter().GetResult().As<WorkflowActivityInstanceDto>();
        }

        /// <summary>
        /// 更新工作流实例状态信息
        /// </summary>
        /// <param name="workflowActivityInstanceId">工作流活动实例Id</param>
        /// <param name="status">活动状态</param>
        /// <returns></returns>
        public WorkflowActivityInstanceDto UpdateActivityStatus(long workflowActivityInstanceId, ActivityInstanceStatus status)
        {
            return workflowActivityInstanceBusinessData.UpdateActivityStatus(workflowActivityInstanceId, status).GetAwaiter().GetResult().As<WorkflowActivityInstanceDto>();
        }

        public WorkflowActivityInstanceReferenceDto GetActivityInstanceReference(long workflowActivityInstanceId)
        {
            var result = this.redisService.TryGet(string.Format("{0}:WorkflowActivityInstanceId:{1}", EntityNameConst.WorkflowActivityInstanceReferenceEntity, workflowActivityInstanceId), () =>
               {
                   WorkflowActivityInstanceReferenceDto dto = this.workflowActivityInstanceBusinessData.GetActivityInstanceReferenceEntity(workflowActivityInstanceId).GetAwaiter().GetResult().As<WorkflowActivityInstanceReferenceDto>();
                   return dto;
               });
            return result;
        }
    }
}
